#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct list
{
    struct list *next;
    int data;
};

typedef struct list list_t;

list_t *MergeList(list_t **head1, list_t **head2)
{
    list_t *head = NULL;
    list_t *tail;
    if (head1 == NULL || head2 == NULL) {
        return head;
    }
    list_t *p1 = *head1;
    list_t *p2 = *head2;
    if (p1 == NULL) {
        return p2;
    }
    if (p2 == NULL) {
        return p1;
    }
    if (p1->data > p2->data) {
        head = p2;
        p2 = p2->next;
    } else {
        head = p1;
        p1 = p1->next;
    }
    tail = head;

    while (p1 != NULL && p2 != NULL) {
        if (p1->data > p2->data) {
            tail->next = p2;
            tail = p2;
            p2 = p2->next;
        } else {
            tail->next = p1;
            tail = p1;
            p1 = p1->next;
        }
    }

    if (p1 != NULL) {
        tail->next = p1;
    }
    if (p2 != NULL) {
        tail->next = p2;
    }
    *head1 = NULL;
    *head2 = NULL;

    return head;
}

void CleanList(list_t *head)
{
    list_t *node = head;
    while (node != NULL) {
        list_t *tmp = node;
        node = node->next;
        free(tmp);
    }
}

void PrintList(list_t *node)
{
    while (node != NULL) {
        if (node->next == NULL) {
            printf("%d", node->data);
        } else {
            printf("%d ", node->data);
        }
        node = node->next;
    }
    //printf("\n");
}

list_t *CreateList(void)
{
    list_t *head = NULL;
    list_t *tail = head;

    int data = 0;
    int c = 0;

    do {
        int ret = scanf("%d", &data);
        if (ret == EOF) {
            break;
        }
        list_t *node = (list_t *)malloc(sizeof(list_t));
        if (node == NULL) {
            continue;
        }
        memset(node, 0, sizeof(list_t));
        node->data = data;
        if (head == NULL) {
            head = node;
            tail = node;
        } else {
            tail->next = node;
            tail = node;
        }
    } while ((c = getchar()) != EOF && c != '\n');

    return head;
}

int main(void)
{
    list_t *head1 = CreateList();
    list_t *head2 = CreateList();
    list_t *head = MergeList(&head1, &head2);
    PrintList(head);
    CleanList(head);
    head = NULL;

    return 0;
}
